package com.kaishengit.service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import com.kaishengit.dao.DocumentDao;
import com.kaishengit.dao.MessageDao;
import com.kaishengit.entity.Comment;
import com.kaishengit.entity.Document;
import com.kaishengit.entity.DocumentComment;
import com.kaishengit.entity.Message;
import com.kaishengit.entity.User;
import com.kaishengit.util.DateUtil;
import com.kaishengit.util.PKutil;
import com.kaishengit.util.Page;
import com.kaishengit.util.SubS;

public class DocumentService {
	private DocumentDao dao = new DocumentDao();
	private MessageDao mdao=new MessageDao();
	private CommentService comService = new CommentService();
	public Page<DocumentComment> findAll(User user, int pageNum,String id) {
		Page<DocumentComment> page = new Page<DocumentComment>();
		page.setPageNum(pageNum);
		page.setPageSize(3);
		page.setTotalCount(dao.findCount(id));
		page.getTotalCount();
		List<Document> list = dao.findAllByProjectId(page.getOffest(),page.getPageSize(),id);
		List<DocumentComment> dclist = new ArrayList<DocumentComment>();
		for(Document document:list){
			DocumentComment dc = new DocumentComment();
			if(document.getContent().length() >200){
				document.setContent(document.getContent().substring(0,198));	
			}
			try {
				String time = DateUtil.getAccount(DateUtil.getNow(),document.getCreatetime());
				document.setTime(time);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			List<Comment> commentlist = comService.findAllByDocumentid(document.getId());
			for(Comment comment:commentlist){
				String time;
				try {
					time = DateUtil.getAccount(DateUtil.getNow(),comment.getCreatetime());
					comment.setTime(time);	
				} catch (ParseException e) {
					e.printStackTrace();
				}
				
			}
			dc.setList(commentlist);
			dc.setDocument(document);
			dclist.add(dc);
		}
		page.setList(dclist);
		return page;
	}
	public Document findDocument(String id) {
		return dao.findbyId(id);
		
	}
	public Document findDocument(String id,String userid) {
		return dao.findbyId(id);
		
	}
	public void save(String title, String content, User user, String projectid) {
		String id = PKutil.getPk();
		String createtime = DateUtil.getNow();
		Document document = new Document();
		document.setId(id);
		document.setCreatetime(createtime);
		document.setUserid(user.getId());
		document.setContent(content);
		document.setTitle(title);
		document.setProjectid(projectid);
		dao.save(document);
		
		/**
		 * 将新资料添加到message表中
		 * */
		Message m=new Message();
		String mid=PKutil.getPk();
		String mtime=DateUtil.getNow();
		m.setId(mid);
		m.setName(title);
		String con=SubS.getSubString(content);
		m.setContent(con);
		m.setUserid(user.getId());
		m.setProjectid(projectid);
		m.setDocumentid(id);
		m.setType("document");
		m.setDate(mtime);
		mdao.saveDocument(m);
		
	}
	public void update(String id, String title, String content) {
		String time=DateUtil.getNow();
		dao.update(id,title,content,time);
		
	}
	public void del(String id) {
		dao.del(id);
		
	}
}
